package com.dayaokeji.rhythmschoolstudent.client.mine.statistics

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.client.common.base.ActivityTemplateMethod
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity
import com.dayaokeji.rhythmschoolstudent.client.mine.statistics.adapter.StatisticDetailAdapter
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback
import com.dayaokeji.rhythmschoolstudent.wiget.WrapLinearLayoutManager
import com.dayaokeji.server_api.ApiUtils
import com.dayaokeji.server_api.ServerResponse
import com.dayaokeji.server_api.api.StatisticsApi
import com.dayaokeji.server_api.domain.StatisticDetail
import com.dayaokeji.server_api.domain.StatisticsDetailParams
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.activity_attendance_statistic_detial.*
import retrofit2.Call

/**
 * 考勤率详情
 */
class AttendanceStatisticDetailActivity : BaseActivity() , ActivityTemplateMethod {

    companion object {
        private const val EXTRA_STATISTICS_DETAIL_PARAMS = "params"

        fun navigation(cxt: Context,statisticsParams: StatisticsDetailParams) {
            val intent = Intent(cxt,AttendanceStatisticDetailActivity::class.java)
            intent.putExtra(EXTRA_STATISTICS_DETAIL_PARAMS,statisticsParams)
            cxt.startActivity(intent)
        }
    }


    private var statisticsDetailParams: StatisticsDetailParams? = null

    private val statisticsDetailAdapter: StatisticDetailAdapter by lazy { StatisticDetailAdapter() }
    private val statisticsApi: StatisticsApi by lazy { ApiUtils.getApi(StatisticsApi::class.java) }
    private var statisticDetailCall: Call<ServerResponse<List<StatisticDetail>>> ? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_attendance_statistic_detial)
        toolbar?.let {
            setSupportActionBar(it)
        }
        init()
    }

    override fun init() {
        statisticsDetailParams = intent.getSerializableExtra(EXTRA_STATISTICS_DETAIL_PARAMS) as StatisticsDetailParams

        if (statisticsDetailParams == null) {
            Logger.d("params is null")
            return
        }

        super.init()
    }

    override fun setupView() {
        rvStatisticsDetails.layoutManager = WrapLinearLayoutManager(this)
        rvStatisticsDetails.adapter = statisticsDetailAdapter

        supportActionBar?.title = statisticsDetailParams?.courseName
    }

    override fun setupViewData() {
        reqStatisticsDetail()
    }

    private fun reqStatisticsDetail() {
        statisticDetailCall = statisticsApi.statisticDetail(statisticsDetailParams)
        statisticDetailCall
        statisticDetailCall?.enqueue(object :ServerCallback<List<StatisticDetail>>(){
            override fun onResponse(success: Boolean, response: ServerResponse<List<StatisticDetail>>?) {
                if (success) {
                    response?.let {
                        val lists: ArrayList<StatisticDetail> = ArrayList<StatisticDetail>()
                        for (statisticDetail in it.body) {
                            statisticDetail.courseName =  statisticsDetailParams?.courseName
                            lists.add(statisticDetail)
                        }
                        statisticsDetailAdapter.setNewData(lists)
                    }
                }
            }
        })

    }

    override fun onDestroy() {
        super.onDestroy()
        statisticDetailCall?.cancel()
    }
}
